package io.github.tangwan.designpattern.creational.singleton;

/**
 * 懒汉-线程安全(重量级锁)
 * @author tangwan
 * @version V1.0
 * created on 2021/9/27 11:20
 * @since JDK 1.8
 */
public class Singleton2 {

    private static Singleton2 instance;

    private Singleton2() {
    }

    //同时只能有一个线程执行下面方法,线程安全
    public static synchronized Singleton2 getInstance() {
        if (instance == null) {
            instance = new Singleton2();
        }
        return instance;
    }

}
